var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var http = require("helper/http");
var imgTool = require("helper/picture");
var voteUrl = require("url/vote");
var helperVote = require("helper/vote");

var addBaseUrl = voteUrl.url.base.create;
var addItemUrl = voteUrl.url.item.create;

/** **************处理页面-头部-begin**************** */
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var headerLeft = ui(addheader + ".headerLeft");// 左上角-返回
headerLeft.on("touch", function() {
	do_App.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "发布投票"
});
header.refreshData();

// 右上角-发布（提交）
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "发布";
headerRight_lableNext.visible = true;
headerRight.on("touch", "", 5000, function() {
	do_Page.hideKeyboard();
	submitAddTopic();
});

/** **************处理页面-头部-end**************** */
var layout_main = ui("do_LinearLayout_main");

var addtopicRemark = layout_main.add("topicRemark",
		"source://view/reuse/form/mutiTextarea.ui", 0, 0);
var topicRemark = ui("topicRemark");
hashdata.addData({
	"textAreaHint" : '投票内容描述'
});
topicRemark.bindData(hashdata);
topicRemark.refreshData();
//
var linearLayout_1 = ui("linearLayout_1");
var linearLayout_2=ui("linearLayout_2");
var do_ALayout_2 = ui("do_ALayout_2");
var isSupportMulti = 2;
var itemCount = 2;
do_ALayout_2.on("touch", function() {
	itemCount++;
	if (itemCount <= 8) {
		ui("voteItem" + itemCount).visible = true;
		linearLayout_2.redraw();
	}
});
/**
 * ********************************************************选项八
 */
var addVoteItem8 = linearLayout_2.add("voteItem8",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem8 = ui("voteItem8");
hashdata.addData({
	"hint" : '选项八'
});
ui(addVoteItem8 + ".camera").visible = false;
voteItem8.visible = false;
voteItem8.bindData(hashdata);
voteItem8.refreshData();

/**
 * ********************************************************选项七
 */
var addVoteItem7 = linearLayout_2.add("voteItem7",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem7 = ui("voteItem7");
hashdata.addData({
	"hint" : '选项七'
});
ui(addVoteItem7 + ".camera").visible = false;
voteItem7.visible = false;
voteItem7.bindData(hashdata);
voteItem7.refreshData();

/**
 * ********************************************************选项六
 */
var addVoteItem6 = linearLayout_2.add("voteItem6",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem6 = ui("voteItem6");
hashdata.addData({
	"hint" : '选项六'
});
ui(addVoteItem6 + ".camera").visible = false;
voteItem6.visible = false;
voteItem6.bindData(hashdata);
voteItem6.refreshData();

/**
 * ********************************************************选项五
 */
var addVoteItem5 = linearLayout_2.add("voteItem5",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem5 = ui("voteItem5");
hashdata.addData({
	"hint" : '选项五'
});
ui(addVoteItem5 + ".camera").visible = false;
voteItem5.visible = false;
voteItem5.bindData(hashdata);
voteItem5.refreshData();

/**
 * ********************************************************选项四
 */
var addVoteItem4 = linearLayout_2.add("voteItem4",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem4 = ui("voteItem4");
hashdata.addData({
	"hint" : '选项四'
});
ui(addVoteItem4 + ".camera").visible = false;
voteItem4.visible = false;
voteItem4.bindData(hashdata);
voteItem4.refreshData();
/**
 * ********************************************************选项三
 */
var addVoteItem3 = linearLayout_2.add("voteItem3",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem3 = ui("voteItem3");
hashdata.addData({
	"hint" : '选项三'
});
voteItem3.visible = false;
ui(addVoteItem3 + ".camera").visible = false;
voteItem3.bindData(hashdata);
voteItem3.refreshData();
/**
 * ********************************************************选项二
 */
var addVoteItem2 = linearLayout_2.add("voteItem2",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem2 = ui("voteItem2");
hashdata.addData({
	"hint" : '选项二'
});
voteItem2.bindData(hashdata);
voteItem2.refreshData();
ui(addVoteItem2 + ".camera").visible = false;

/**
 * ********************************************************选项一
 */
var addVoteItem1 = linearLayout_2.add("voteItem1",
		"source://view/vote/add/addVoteCell.ui", 0, 0);
var voteItem1 = ui("voteItem1");
hashdata.addData({
	"hint" : '选项一'
});
voteItem1.bindData(hashdata);
voteItem1.refreshData();
ui(addVoteItem1 + ".camera").visible = false;

var addVoteItems = [ addVoteItem1, addVoteItem2, addVoteItem3, addVoteItem4,
		addVoteItem5, addVoteItem6, addVoteItem7, addVoteItem8 ];

/**
 * ********************************************************设置最大多选
 */
var addMultiMaxNum = linearLayout_2.add("multiMaxNum",
		"source://view/reuse/form/mutiInput.ui", 0, 0);
var multiMaxNum = ui("multiMaxNum");
hashdata.addData({
	"textTitle" : '最大多选项',
	"inputType" : "PHONENUMBER",
	"hint" : '1'
});
multiMaxNum.bindData(hashdata);
multiMaxNum.refreshData();
multiMaxNum.visible = false;
var textInput = ui(addMultiMaxNum + ".textInput");
textInput.text = 1;

var isItemImg = 0;
/**
 * ********************************************************switch
 */
var switchtxt2 = linearLayout_1.add("txtswitch2",
		"source://view/reuse/form/mutiSwitch.ui");
var txtswitch2 = ui("txtswitch2");
//var labelTitle = ui(switchtxt + ".labelTitle");
//var aLayoutRoot = ui(switchtxt + ".aLayoutRoot");
txtswitch2.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "是否图片投票"
})
txtswitch2.refreshData();
var swtxt2 = [ 0, " ", " " ]; // 0:开关状态 1:关闭状态文字 2:开启状态文字
txtswitch2.fire("switchzt", swtxt2);

txtswitch2.on("switchtag", function(data) {
	if (data == 0) {// 关闭
		isItemImg = 0;
		foreachVoteItem("camera", false);
	}
	if (data == 1) {// 打开
		isItemImg = 1;
		foreachVoteItem("camera", true);
	}
});

function foreachVoteItem(name, isVisible) {
	for (var i = 0; i < addVoteItems.length; i++) {
		var addVoteItem = addVoteItems[i];
		ui(addVoteItem + "." + name).visible = isVisible;
	}
	linearLayout_2.redraw();
}

/**
 * ********************************************************switch
 */
var switchtxt = linearLayout_1.add("txtswitch",
		"source://view/reuse/form/mutiSwitch.ui");
var switchtxtS = ui("txtswitch");
//var labelTitle = ui(switchtxt + ".labelTitle");
//var aLayoutRoot = ui(switchtxt + ".aLayoutRoot");
switchtxtS.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "是否多选投票"
})
switchtxtS.refreshData();
var swtxt = [ 0, " ", " " ]; // 0:开关状态 1:关闭状态文字 2:开启状态文字
switchtxtS.fire("switchzt", swtxt);

switchtxtS.on("switchtag", function(data) {
	if (data == 0) {// 关闭
		isSupportMulti = 2;
		multiMaxNum.visible = false;
	}
	if (data == 1) {// 打开
		isSupportMulti = 1;
		multiMaxNum.visible = true;
	}
});

/** **************处理-图片显示UI-end**************** */
var imgMax = 9;
var imgGridView = ui("imgGridView");
var imgsData = []; //保存图片数组
imgGridView.on("touch", function(data, e) {
	imgMax=imgMax+1;
	imgsData.splice(data,1);
	imgGridView.items = imgsData;
});
/** **************处理-图片显示UI-end**************** */

/** **************点击-添加图片-begin**************** */
var aLayoutImg = ui("aLayoutImg");
aLayoutImg.on("touch", function(data, e) {
	if (0== imgMax) {
		return;
	}
	imgTool.imgMulti(imgMax, selectImgBack);// 选择多张图片
});

function selectImgBack(imgList) {
	imgMax=imgMax-imgList.length;
	imgsData = imgsData.concat(imgList.map(function(path) {
		return {
			"imgPath" : path
		}
	}));
	imgGridView.items = imgsData;
}
/** **************点击-添加图片-end**************** */

// 上个页面转过来的参数
var bizzType,circleId;

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

// 页面加载完毕后
do_Page.on("loaded", function() {
	var data = do_Page.getData();
	circleId=data.circleId;
	bizzType = data.bizzType;
});
ui("do_ScrollView_2").redraw();
// 提交-按钮
function submitAddTopic() {
	var topicRemarkText = ui(addtopicRemark + '.textArea')
	var contentText = topicRemarkText.text;
	if (imgsData.length == 0 && contentText.length == 0) {
		nf.toast("内容和图片不能都为空!");
		return;
	}
	var itemRemark = [];
	var itemImgs = [];
	var hasItemCount = 0;
	for (var i = 0; i < addVoteItems.length; i++) {
		var textFieldRemark = ui(addVoteItems[i] + ".textFieldRemark");
		var img = ui(addVoteItems[i] + ".imageView");
		if (null != textFieldRemark.text && '' != textFieldRemark.text) {
			hasItemCount++;
			itemRemark.push(textFieldRemark.text);
			if (isItemImg == 1) {
				if (null != img.tag && '' != img.tag) {
					itemImgs.push(img.tag);
				} else {
					nf.toast("投票" + (i+1) + "请上传图片");
					return;
				}
			}
		}
		if (null != img.tag && '' != img.tag) {
			if (null == textFieldRemark.text || ''== textFieldRemark.text) {
				nf.toast("投票" + (i+1) + "请填写描述");
				return;
			}
		}
	}
	if (hasItemCount < 2) {
		nf.toast("不能小于2个投票选项");
		return;
	}

	var imgFiles = [];
	for (var i = 0; i < imgsData.length; i++) {
		imgFiles.push(imgsData[i].imgPath);
	}
	var imgFilesLength = 0;
	imgFilesLength = imgFiles.length;
	
	imgFiles=imgFiles.concat(itemImgs);
	if (imgFiles.length > 0) {
		http.form(imgFiles, "vote", function(imgDatas) {
			var string4imgs = imgDatas.path;// 返回图片云端路径（后部份）用分号隔开
			var imgFiles2 = [];
			var itemImgs2 = [];
			var imgs=string4imgs.split(";");
			for (var i = 0; i < imgs.length; i++) {
				var pathImg = imgs[i];
				if(i<imgFilesLength){
					imgFiles2.push(pathImg);
				}else{
					itemImgs2.push(pathImg);
				}
				
			}
			var topic = {
				"circleId" : circleId,
				"content" : contentText,
				"img" : imgFiles2.join(";"),
				"bizzType" : bizzType,
				"isSupportMulti" : isSupportMulti,
				"multiMaxNum" : textInput.text,
				"itemRemark" : itemRemark.join(";"),
				"itemImgs" : itemImgs2.join(";")
			};
			  http.post(addBaseUrl, topic, function(data) { 
				  nf.toast("发布成功");
				  do_App.closePage({"topic_add_vote":true});
			  });
			 
		});
	} else {
		var topic = {
			"circleId" : circleId,
			"content" : contentText,
			"bizzType" : bizzType,
			"img" : "",
			"isSupportMulti" : isSupportMulti,
			"multiMaxNum" : textInput.text,
			"itemImgs" : "",
			"itemRemark" : itemRemark.join(";")
		};
		http.post(addBaseUrl, topic, function(data) {
			nf.toast("发布成功");
			do_App.closePage({"topic_add_vote":true});
		});
	}
}
